package com.zxk.service;

import com.sun.jna.platform.win32.WinDef;
import com.zxk.constant.JNAConstants;
import com.zxk.constant.ParamConstants;
import com.zxk.constant.PicConstants;
import com.zxk.entity.FindPicResult;
import com.zxk.enums.Coord;
import com.zxk.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 任务抽象类
 *
 * @author: 舟小亢
 * @date: 2023-08-13 13:35
 */
public class AbstractMyTask implements MyTask {

    private static Logger LOG = LoggerFactory.getLogger(AbstractMyTask.class);

    protected FindPicResult pic;

    /**
     * 截图时用句柄
     */
    protected WinDef.HWND hwnd_0;

    /**
     * 执行键鼠命令时用句柄
     */
    protected WinDef.HWND hwnd_1;

    /**
     * 备用句柄
     */
    protected WinDef.HWND hwnd_2;

    public AbstractMyTask(WinDef.HWND hwnd_0, WinDef.HWND hwnd_1, WinDef.HWND hwnd_2) {
        this.hwnd_0 = hwnd_0;
        this.hwnd_1 = hwnd_1;
        this.hwnd_2 = hwnd_2;
    }

    @Override
    public boolean goHome() throws Exception {
        // 1 初始化状态
        if (!iniState(ParamConstants.UNIFY_TIMES, 5000)) {
            return false;
        }

        // 2 点击地图回到长安城
        if (returnChangAn()) {
            return false;
        }
        return true;
    }


    @Override
    public boolean execute() throws Exception {
        return true;
    }

    @Override
    public boolean checkTask() throws Exception {
        return true;
    }

    @Override
    public String getTaskName() throws Exception {
        return "未知的任务";
    }

    /**
     * 父类特有初始化状态方法
     *
     * @param cnt    初始化次数
     * @param dcTime 双重确认等待时间
     * @return
     */
    protected boolean iniState(int cnt, long dcTime) throws Exception {
        LOG.info("【初始化状态】开始");
        cnt = cnt <= 0 ? 1 : cnt;
        for (int i = 0; i < cnt; i++) {
            // 循环摁ESC，直到找到地图
            pic = PixelPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 1);
            if (pic.isFinded()) {
                // 双重查找,保证准确找到
                ThreadUtil.sleep(dcTime);
                pic = PixelPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 1);
                if (pic.isFinded()) {
                    LOG.info("【初始化状态】结束");
                    return true;
                }
            }
            KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
        }
        // 如果没找到，则退出
        LOG.error("【初始化状态】失败");
        return false;
    }

    /**
     * 切换助战组合
     *
     * @param coord 助战组合位置
     * @return
     */
    protected boolean changeAssistantTeam(Coord coord) throws Exception {
        // 切换助战组合2
        if (FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_更多选项, 10) &&
                FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_助战按钮, 5) &&
                FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_助战界面, 5).isFinded()) {
            MouseUtil.lClickRandom(hwnd_1, coord.getX(), coord.getY());
            for (int i = 0; i < 10; i++) {
                KeyUtil.kClick(hwnd_1, JNAConstants.VK_ESCAPE);
                // 切换成功，直到找到活动后退出
                if (FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_活动, 3).isFinded()) {
                    // 取消更多选项
                    FuzzyPicUtil.findAndClickRandom(hwnd_0, hwnd_1, PicConstants.公共_取消更多选项, 5);
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 回到长安城
     */
    protected boolean returnChangAn() throws Exception {
        LOG.info("【回长安城】开始");
        // 点击主地图(点击两次，保证成功)
        MouseUtil.lClickRandom(hwnd_1, Coord.MAJOR_MAP_XY.getX(), Coord.MAJOR_MAP_XY.getY());
        ThreadUtil.sleep1();
        MouseUtil.lClickRandom(hwnd_1, Coord.MAJOR_MAP_XY.getX(), Coord.MAJOR_MAP_XY.getY());
        // 点击地图右滑，确保能找到长安城
        MouseUtil.lClickRandom(hwnd_1, Coord.MAP_SLIDE.getX(), Coord.MAP_SLIDE.getY());
        // 找到并点击长安城大地图
        pic = FuzzyPicUtil.findPic(hwnd_0, PicConstants.公共_长安城大图标, 15, 0.5);
        if (!pic.isFinded()) {
            return true;
        }
        MouseUtil.lClickRandom(hwnd_1,
                pic.getPicX() + Coord.CHANGAN_MAP_DXY.getX(),
                pic.getPicY() + Coord.CHANGAN_MAP_DXY.getY());

        // 确认切换成功
        pic = PixelPicUtil.findPic(hwnd_0, PicConstants.公共_长安城小图标, 10);
        if (!pic.isFinded()) {
            LOG.error("切换地图长安城失败");
            return true;
        }
        LOG.info("【回长安城】开始");
        return false;
    }

    /**
     * 退出队伍
     */
    protected boolean exitTeam() throws Exception {


        return true;
    }
}
